package com.zangs.emqx_expand.web.controller.emqx;

import com.zangs.emqx_expand.common.annotation.SignatureApi;
import com.zangs.emqx_expand.common.core.controller.BaseController;
import com.zangs.emqx_expand.common.core.domain.AjaxResult;
import com.zangs.emqx_expand.emqx.domain.MqttAcl;
import com.zangs.emqx_expand.emqx.domain.MqttUser;
import com.zangs.emqx_expand.emqx.domain.MqttUserOnline;
import com.zangs.emqx_expand.emqx.service.MQTTService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/emqx")
@Api("emqxApi接口")
public class ApiController extends BaseController {
    @Resource
    private MQTTService mqttService;

    /**
     * 新增规则
     *
     * @param userUid     用户uid(群组uid)
     * @param mqttAcl 规则
     */
    @ApiOperation("新增规则接口")
    @SignatureApi(remark = "新增规则")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userUid", value = "用户UID", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "action", value = "操作类型('subscribe', 'publish', 'all')", dataType = "string"),
            @ApiImplicitParam(name = "userId", value = "用户Id", dataType = "long"),
            @ApiImplicitParam(name = "clientId", value = "客户端ID", dataType = "string")
    })
    @PostMapping("/acl/{userUid}")
    public AjaxResult addAcl(@PathVariable("userUid") String userUid, @Valid @RequestBody List<MqttAcl> mqttAcl) {
        return mqttService.addAcl(userUid, mqttAcl);
    }

    /**
     * 修改规则名
     *
     * @param topicUid 主题uid
     */
    @ApiOperation( "修改规则接口(修改主题名)")

    @SignatureApi(remark = "修改规则名")
    @GetMapping("/acl")
    public AjaxResult editAcl(@RequestParam(name = "topicUid") String topicUid, @RequestParam(name = "topic") String topic) {

        return mqttService.editAclTopicByUid(topicUid, topic);
    }

    /**
     * 修改规则里主题的前缀
     *
     * @param prefix    原来的前缀
     * @param newPrefix 新的前缀
     * @return
     */
    @ApiOperation( "修改规则接口(修改主题前缀)")

    @GetMapping("/acl/topic/{prefix}/{newPrefix}")
    public AjaxResult editAclByPrefix(@PathVariable("prefix") String prefix, @PathVariable("newPrefix") String newPrefix) {
        return toAjax(mqttService.editAclByPrefix(prefix, newPrefix));
    }


    /**
     * 根据clientIds删除所有规则
     */
    @ApiOperation( "通过clientIds删除规则")
    @SignatureApi(remark = "根据clientIds删除所有规则")
    @DeleteMapping("/acl/{clientIds}")
    public AjaxResult deleteAcl(@PathVariable("clientIds") String[] clientIds) {
        return toAjax(mqttService.deleteAllBy(clientIds));
    }


    /**
     * 新增用户
     *
     * @param mqttUser 用户信息
     */
    @ApiOperation( "新增用户")
    @SignatureApi(remark = "新增用户")
    @PostMapping("/user")
    public AjaxResult addUser(@Valid @RequestBody MqttUser mqttUser) {
        mqttUser.setIsSuperuser(false);
        return mqttService.addUser(mqttUser);
    }

    /**
     * 编辑用户
     * 通过uid锁定用户
     *
     * @param mqttUser 需要修改的用户信息
     */
    @ApiOperation( "修改用户")
    @SignatureApi(remark = "编辑用户")
    @PutMapping("/user")
    public AjaxResult editUser(@RequestBody MqttUser mqttUser) {
        return mqttService.editUser(mqttUser);
    }

    /**
     * 删除用户(同时会一起删除规则和在线记录表)
     *
     * @param userUids 需要删除的用户uid
     */
    @ApiOperation( "删除用户")

    @SignatureApi(remark = "删除用户(同时会一起删除规则和在线记录表)")
    @DeleteMapping("/user/{userUids}")
    public AjaxResult deleteUser(@PathVariable String[] userUids) {

        return mqttService.deleteUser(userUids);

    }

    /**
     * 如果没有就新增 如果存在就修改
     *
     * @param mqttUserOnline
     * @return
     */
    @ApiOperation( "新增在线记录")

    @SignatureApi(remark = "新增在线记录)")
    @PostMapping("/userOnline")
    public AjaxResult addUserStatus(@Valid @RequestBody List<MqttUserOnline> mqttUserOnline) {

        return toAjax(mqttService.addUserStatus(mqttUserOnline));

    }

    @ApiOperation( "修改在线记录")
    @SignatureApi(remark = "修改在线记录)")
    @PutMapping("/userOnline")
    public AjaxResult editUserStatus(@RequestBody List<MqttUserOnline> mqttUserOnline) {
        return mqttService.editUserStatusClientId(mqttUserOnline);
    }

    @ApiOperation( "删除在线记录")
    @SignatureApi(remark = "删除在线记录)")
    @DeleteMapping("/userOnline/{userUid}/{ClientIds}")
    public AjaxResult deleteUserStatus(@PathVariable("userUid") String userUid, @PathVariable String[] ClientIds) {

        return toAjax(mqttService.deleteMqttUserOnline(userUid, ClientIds));

    }

}
